package com.imau.common.dataSourceTool;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.InputStream;

public class SqlSessionFactoryUtil2 {
    //① 读取配置文件

    private static String resource = "mybatis-config.xml";


    //单例模式获取SQLSession
    private static SqlSessionFactory sqlSessionFactory = null;


    private SqlSessionFactoryUtil2() {

    }


    public static SqlSessionFactory getFactory() {


        if(sqlSessionFactory == null)
        {
            try {
                InputStream inputStream = Resources.getResourceAsStream(resource);

                MybatisSqlSessionFactoryBuilder mybatisSessionFactoryBuilder = new MybatisSqlSessionFactoryBuilder();

                //通过配置文件获取session工厂
                 sqlSessionFactory = mybatisSessionFactoryBuilder.build(inputStream);
                //sqlSessionFactory =  new SqlSessionFactoryBuilder().build(inputStream);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return sqlSessionFactory;
    }

}
